<?php
/**
 * Created by PhpStorm.
 * User: hasee
 * Date: 2018/3/21
 * Time: 11:06
 */

namespace frontend\controllers;

use Yii;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use \yii\web\Response;
use yii\helpers\Html;
use common\models\Registration;
use common\models\GenerateRecords;
use common\models\InvitationCode;
use common\models\Character;

class SummitController extends Controller
{   
    public $enableCsrfValidation = false;
    public function actionLogin()
    {    $this->layout = false;
        return $this->render('login');
    }

    public function actionCode()
    {
        return $this->render('code');
    }

    public function actionComplete()
    {
         $this->layout = false;
        return $this->render('complete');
    }
	public function actionComplete2(){
		 $this->layout = false;
		return $this->render('complete2'); 
	 }
	 public function actionComplete3(){
		 $this->layout = false;
		return $this->render('complete3'); 
	 }
//    public function actionTest()
//    {
//        $model = Registration::findOne(['invitation_code'=>'103823']);
//
//        return $this->render('index',['model'=>$model]);
//    }


    public function actionVerify($code=null)
    {
        $verify = InvitationCode::verificationCode($code);
        if (!$verify){
            Yii::$app->response->format = Response::FORMAT_JSON;
            return ['code'=>50,'msg'=>'邀请码错误'];
        }

        Yii::$app->response->format = Response::FORMAT_JSON;
        return ['code'=>200,'msg'=>'邀请码正确','id'=>$code];
    }



    public function actionSignup($code=null)
    {
      if(empty($code)){
             $this->layout = false;
             return $this->redirect('viewer');
        }
		
		
		$res=Registration::find()->where(['invitation_code' => $code])->all();
		
		if(empty($res)){
			$model=new  Registration;
			$model->invitation_code = $code;
		$model->save();
		}
		$role = $this->findRole($code);
		if(empty($res)){
			$model->character = $role;
		$model->save();
		}
		
         if($role=='VIP陪同'){
			 
			$this->redirect(['summit/vippt','code'=>$code]);

         }else if($role=='VIP'){
			
			$this->redirect(['summit/vip','code'=>$code]);
            
         }
         elseif($role=='嘉宾（不包吃住）'){
             
			 
			 $this->redirect(['summit/vipbb','code'=>$code]);
            
         }


         

        
    }

 public function actionViewer( )
    {
      
        $model = new Registration;
		$character='观众';
     // var_dump($model);exit;
        if ($model->load(Yii::$app->request->post())){
           // var_dump(Yii::$app->request->post());exit;
            if($model->save()){
				$id = $model->attributes['id'];
				$model->character = $character;
				$model->save();
				//$id=Yii::$app->db->getLastInsertedID();
				//var_dump($id);exit;
                //return $this->redirect('forum,');
				$this->redirect(['summit/forum2','id'=>$id]);
            }
             
           } 
    return $this->render('viewer',[
            'model' => $model,
			 'character' => $character
            
        ]);
         

        
    }


     public function actionVippt(){
        $code=Yii::$app->request->get('code');;

        $model = $this->findModel($code);
        if ($model->load(Yii::$app->request->post())){
            
            if($model->save()){
			 // $this->layout = false;
			$this->redirect(['summit/traffic','code'=>$code]);
              // return $this->redirect('traffic','code'=>$code);
            }
             
           } 
              
        
        return $this->render('vippt',[
            'model' => $model,
             'code' => $code
        ]);
       
     }


     public function actionVip(){
       $code=Yii::$app->request->get('code');;

        $model = $this->findModel($code);
    
      //var_dump($model);eixt;
        if ($model->load(Yii::$app->request->post())){
            
            if($model->save()){
				$this->redirect(['summit/traffic','code'=>$code]);
               // return $this->redirect('traffic','code'=>$code);
            }
             
           } 
              
        
        return $this->render('vip',[
            'model' => $model,
             'code' => $code
        ]);
       
     }
      public function actionVipbb(){
       $code=Yii::$app->request->get('code');;

        $model = $this->findModel($code);
    
      
        if ($model->load(Yii::$app->request->post())){
            
            if($model->save()){
				$this->redirect(['summit/forum','code'=>$code]);
                //return $this->redirect('traffic','code'=>$code);
            }
             
           } 
              
        
        return $this->render('vipbb',[
            'model' => $model,
             'code' => $code
        ]);
       
     }

      public function actionTraffic(){
		   $this->layout = false;
         $code=Yii::$app->request->get('code');;

        $model = $this->findModel($code);
    
      
        if ($model->load(Yii::$app->request->post())){
            
            if($model->save()){

                 $role = $this->findRole($code);
                if($role=='VIP陪同'){
					$this->redirect(['summit/traffic2','code'=>$code]);
                   // return $this->redirect('traffic2','code'=>$code);
                }elseif($role=='VIP'){
					$this->redirect(['summit/traffic2','code'=>$code]);
                    //return $this->redirect('traffic2','code'=>$code);
                }

                 
            }
             
           } 
              
        
        return $this->render('traffic',[
            'model' => $model,
             'code' => $code
        ]);
       
     }
      public function actionTraffic2(){
		   $this->layout = false;
       $code=Yii::$app->request->get('code');;

        $model = $this->findModel($code);
    
      
        if ($model->load(Yii::$app->request->post())){
            
            if($model->save()){

                 $role = $this->findRole($code);
                if($role=='VIP陪同'){
					$this->redirect(array('food','code'=>$code)); 
                    //return $this->redirect('food','code'=>$code);
                }elseif($role=='VIP'){
					$this->redirect(array('food','code'=>$code));
                    //return $this->redirect('food','code'=>$code);
                }
            }
             
           } 
              
        
        return $this->render('traffic2',[
            'model' => $model,
             'code' => $code
        ]);
       
     }

     public function actionFood(){
       $code=Yii::$app->request->get('code');;

        $model = $this->findModel($code);
    
      
        if ($model->load(Yii::$app->request->post())){
            
            if($model->save()){

                 $role = $this->findRole($code);
                if($role=='VIP陪同'){
					$this->redirect(array('forum','code'=>$code));
                   // return $this->redirect('forum','code'=>$code);
                }elseif($role=='VIP'){
					$this->redirect(array('hotel','code'=>$code));
                    //return $this->redirect('hotel','code'=>$code);
                }
            }
             
           } 
              
        
        return $this->render('food',[
            'model' => $model,
             'code' => $code
        ]);
       
     }
      public function actionForum(){
		   $this->layout = false;
        $code=Yii::$app->request->get('code');
		

        $model = $this->findModel($code);
		
      
        if ($model->load(Yii::$app->request->post())){
            
            if($model->save()){
				
				$role = $this->findRole($code);
                if($role=='VIP陪同'){
					 return $this->redirect('complete');
                }elseif($role=='VIP'){
					 return $this->redirect('complete');
                    
                }else{
					 return $this->redirect('complete3');
				}
				
				
				
				
				
                
            }
             
           } 
              
        
        return $this->render('forum',[
            'model' => $model,
             'code' => $code
        ]);
       
     }
	 public function actionForum2(){
		  $this->layout = false;
        $id=Yii::$app->request->get('id');
		

        $model =Registration::findOne($id);
		
      
        if ($model->load(Yii::$app->request->post())){
            
            if($model->save()){
				 return $this->redirect('complete2');
                
            }
             
           } 
              
        
        return $this->render('forum',[
            'model' => $model,
             'id' => $id
        ]);
       
     }
	 
     public function actionHotel(){
       $code=Yii::$app->request->get('code');;

        $model = $this->findModel($code);
    
      
        if ($model->load(Yii::$app->request->post())){
            
            if($model->save()){

                 $role = $this->findRole($code);
                if($role=='VIP陪同'){
					$this->redirect(['summit/food','code'=>$code]);
                   // return $this->redirect('food','code'=>$code);
                }elseif($role=='VIP'){
					$this->redirect(['summit/forum','code'=>$code]);
                    //return $this->redirect('forum','code'=>$code);
                }
            }
             
           } 
              
        
        return $this->render('hotel',[
            'model' => $model,
             'code' => $code
        ]);
       
     }






    protected function findModel($code)
    {
        if (empty($code))
        {
            $model = new Registration;
            //return $model;
        }else{
			//$query = new Registration;
			//$model = Registration::find()->where(['invitation_code'=>$code])->one();
			$model = Registration::findOne(['invitation_code'=>$code]);
		}

       // var_dump($model);exit;

        return $model;
    }

     protected function findRole($code)
    {
         $model = Registration::findModel($code);
		//var_dump($model);exit;
         $generate_id=$model->generate_id;

         $Character=GenerateRecords::findOne($generate_id);
         $roleid=$Character->character_id;
		 
         $rolearr=character::findOne($roleid);
		  
         $role=$rolearr->name;
		 //var_dump($role);exit;
         return $role;
    }






     
     
}